package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.EdocCategory;
import com.ktjy.pojo.EdocEntry;
import com.ktjy.service.IEdocCategoryService;
import com.ktjy.service.IEdocEntryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hjy
 * @since 2025-01-08
 */
@Controller
@RequestMapping("/edocEntry")
public class EdocEntryController extends BaseController {
    Logger logger= LoggerFactory.getLogger(EdocEntryController.class);

    @Autowired
    IEdocCategoryService edocCategoryService;
    @Autowired
    IEdocEntryService edocEntryService;

    @RequestMapping("/page")
    public String page(EdocEntry entry,
                       @RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize,
                       Model model){
        try {
            Page page=new Page(pageNo,pageSize);
            List<EdocCategory> list=edocCategoryService.list();
            QueryWrapper queryWrapper=new QueryWrapper();
            if (entry.getCategoryId()!=null){
                queryWrapper.eq("categoryId",entry.getCategoryId());
            }
            Page<EdocEntry> pageInfo=edocEntryService.page(page,queryWrapper);
            model.addAttribute("pageInfo",pageInfo);
            model.addAttribute("entry",entry);
            model.addAttribute("list",list);
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 page 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "page";
    }

    @GetMapping("/save")
    public String save(Model model){
        List<EdocCategory> list=edocCategoryService.list();
        model.addAttribute("list",list);
        model.addAttribute("entry",new EdocEntry());
        return "save";
    }
    @PostMapping("/save")
    public String dosave(EdocEntry entry,Model model){
        try {
            boolean ok=false;
            if (entry.getId()==null){
                ok=edocEntryService.save(entry);
            }else {
                ok=edocEntryService.updateById(entry);
            }
            if (ok){
                return redirect("/edocEntry/page");
            }
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 dosave 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "save";
    }

    @GetMapping("/edit")
    public String edit(@RequestParam("id") Integer id,Model model){
        try {
            List<EdocCategory> list=edocCategoryService.list();
            EdocEntry entry1=edocEntryService.getById(id);
            model.addAttribute("entry",entry1);
            model.addAttribute("list",list);
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 edit 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "save";
    }

    @GetMapping("/view/{id}")
    public String view(@PathVariable("id") Integer id, Model model){
        try {
            EdocEntry entry1=edocEntryService.getById(id);
            model.addAttribute("entry",entry1);
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 view 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return "view";
    }

    @DeleteMapping("/del/{id}")
    @ResponseBody
    public Object del(@PathVariable("id") Integer id){
        Map<String,Object> map=new HashMap<>();
        try {
            boolean ok=edocEntryService.removeById(id);
            if (ok){
                map.put("code",200);
                map.put("msg","删除成功");
            }else {
                map.put("code",500);
                map.put("msg","删除失败");
            }
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 del 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return map;
    }

    @GetMapping("/titleExist")
    @ResponseBody
    public Object titleExist(@RequestParam("title") String title){
        Map<String,Object> map=new HashMap<>();
        try {
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("title",title);
            EdocEntry entry1=edocEntryService.getOne(queryWrapper);
            if (entry1!=null){
                map.put("exist",1);
            }else {
                map.put("exist",0);
            }
        }catch (Exception e) {
            logger.error("EdocEntryController 中的 titleExist 方法执行发生错误。{} ",e);
            throw new RuntimeException(e);
        }
        return map;
    }
}
